import MetaMaskOnboarding from "@metamask/onboarding";
const currentUrl = new URL(window.location.href);
const forwarderOrigin =
  currentUrl.hostname === "localhost" ? "http://localhost:9010" : undefined;
const { isMetaMaskInstalled } = MetaMaskOnboarding;

export default class MetaMask {
  static connected = false;
  // static gmsContractAddress = "0xacB11D676BfA610261c414CC110D9182d79b8682";
  // static gmsContractFactory = null;
  // static gmsEthersProvider = null;
  // static oContractAddress = "0x2b6873Fe7cd2850eF42550c78Ad53FC24bE80228";
  // static oContractFactory = null;

  static init() {
    if (MetaMask.connected) {
      return 1;
    }
    // try {
    //   MetaMask.gmsEthersProvider = new ethers.providers.Web3Provider(
    //     window.ethereum,
    //     "any"
    //   );
    //   MetaMask.gmsContractFactory = new ethers.Contract(
    //     MetaMask.gmsContractAddress,
    //     GmsERC721ABI,
    //     MetaMask.gmsEthersProvider.getSigner()
    //   );
    //   MetaMask.oContractFactory = new ethers.Contract(
    //     MetaMask.oContractAddress,
    //     oERC721ABI,
    //     MetaMask.gmsEthersProvider.getSigner()
    //   );

    //   console.log(MetaMask.oContractFactory);
    // } catch (error) {
    //   console.error(error);
    // }

    let onboarding;
    try {
      onboarding = new MetaMaskOnboarding({ forwarderOrigin });
    } catch (error) {
      console.error(error);
    }

    function handleNewChain(chainId) {
      console.log("handleNewChain is " + chainId);
    }
    function handleEIP1559Support(supported) {
      if (supported && Array.isArray(accounts) && accounts.length >= 1) {
        console.log("is supported EIP1559");
      } else {
        console.log("is unsupported EIP1559");
      }
    }
    function handleNewNetwork(networkId) {
      console.log("handleNewNetwork is " + networkId);
      window.location.reload();
    }
    function handleChangeAccounts(newAccounts) {
      console.log("handleNewChain is " + newAccounts);
      window.location.reload();
    }

    if (isMetaMaskInstalled()) {
      ethereum.autoRefreshOnNetworkChange = false;

      ethereum.on("chainChanged", (chain) => {
        handleNewChain(chain);
        ethereum
          .request({
            method: "eth_getBlockByNumber",
            params: ["latest", false],
          })
          .then((block) => {
            handleEIP1559Support(block.baseFeePerGas !== undefined);
          });
      });
      ethereum.on("chainChanged", handleNewNetwork);
      ethereum.on("accountsChanged", (newAccounts) => {
        ethereum
          .request({
            method: "eth_getBlockByNumber",
            params: ["latest", false],
          })
          .then((block) => {
            handleEIP1559Support(block.baseFeePerGas !== undefined);
          });
        handleChangeAccounts(newAccounts);
      });

      MetaMask.connected = true;

      return 1;
    } else {
      // console.log("MetaMask is not installed!");
      // onboarding.startOnboarding();
      return 0;
    }
  }

  static async connect() {
    return ethereum.request({
      method: "eth_requestAccounts",
    });
  }

  static async getAccount() {
    return ethereum.request({
      method: "eth_accounts",
    });
  }

  static async requestPermissions(from) {
    return ethereum.request({
      method: "wallet_requestPermissions",
      params: [{ eth_accounts: {} }],
    });
  }

  static async getPermissions() {
    return ethereum.request({
      method: "wallet_getPermissions",
    });
  }

  static async transaction(orderid, from, to, value) {
    console.log(value);
    let params = [
      {
        from: from,
        to: to,
        value: value,
      },
    ];

    return ethereum.request({
      method: "eth_sendTransaction",
      params: params,
    });
  }
}
